package com.ruoyi.sample.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.experiment.domain.Circulation;
import com.ruoyi.experiment.domain.Project;
import com.ruoyi.experiment.domain.Sample;
import com.ruoyi.experiment.model.TreeSample;
import com.ruoyi.sample.model.SItem;
import com.ruoyi.sample.model.SampleLz;
import com.ruoyi.sample.service.ISampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 样品Controller
 *
 * @author ruoyi
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/sample")
public class SampleController extends BaseController
{
    @Autowired
    private ISampleService sampleService;

    /**
     * 查询样品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Sample sample)
    {
        startPage();
        List<TreeSample> list = sampleService.selectTreeSampleList(sample);
        return getDataTable(list);
    }

    /**
     * 确认流转
     * @param id
     * @return
     */
    @GetMapping("/c")
    public AjaxResult confirm(Long id){
        boolean b = sampleService.confirmLZ(0, id);
        return success(b);
    }
    /**
     * 添加待确认
     * @param id
     * @return
     */
    @GetMapping("/c/add")
    public AjaxResult addConfirm(Long id,Integer status){
        boolean b = sampleService.confirmLZ(status, id);
        return success(b);
    }

    /**
     * 导出样品列表
     */
    @Log(title = "样品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sample sample)
    {
        List<TreeSample> list = sampleService.selectTreeSampleList(sample);
        List<Sample> samples = sampleService.transformTreeToList(list);
        ExcelUtil<Sample> util = new ExcelUtil<Sample>(Sample.class);
        util.exportExcel(response, samples, "样品数据");
    }

    /**
     * 获取样品详细信息
     */
    @GetMapping(value = "/{sampleId}")
    public AjaxResult getInfo(@PathVariable("sampleId") Long sampleId)
    {
        return success(sampleService.selectSampleBySampleId(sampleId));
    }

    /**
     * 校验样品编号合法性
     * @param projectId
     * @param name
     * @return
     */
    @GetMapping("/name")
    public AjaxResult checkSampleName(Long projectId,String name){
        boolean b = sampleService.checkSampleName(projectId, name);
        return success(b);
    }

    /**
     * 新增样品
     */
    @Log(title = "样品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sample sample)
    {
        return toAjax(sampleService.insertSample(sample));
    }

    /**
     * 批量导入样品
     * @param samples
     * @return
     */
    @PostMapping("import")
    public AjaxResult importSamples(@RequestBody List<Sample> samples){
        int i;
        try {
            i = sampleService.addSamples(samples);
        }catch (Exception e) {
            return AjaxResult.error("导入数据有误");
        }
        if (i==-1)
            return AjaxResult.error("样品编号重复");
        else if (i == -2) {
            return AjaxResult.error("项目已结束");
        }
        return success();
    }

    /**
     * 修改样品
     */
    @Log(title = "样品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sample sample)
    {
        return toAjax(sampleService.updateSample(sample));
    }

    /**
     * 删除样品
     */
    @Log(title = "样品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sampleIds}")
    public AjaxResult remove(@PathVariable Long[] sampleIds)
    {
        return toAjax(sampleService.deleteSampleBySampleIds(sampleIds));
    }

    @GetMapping("/status/{sampleIds}")
    public AjaxResult getStatus(@PathVariable Long[] sampleIds){
        return success(sampleService.selectSampleStatusBySampleIds(sampleIds));
    }

    @GetMapping("item")
    public TableDataInfo listItem(Project project){
        startPage();
        List<SItem> sItems = sampleService.selectAllItem(project);
        return getDataTable(sItems);
    }

    @PostMapping("flume")
    public AjaxResult flume(@RequestBody Circulation circulation){
        sampleService.flume(circulation);
        return success();
    }
    @PostMapping("flumeback")
    public AjaxResult flumeback(@RequestBody Circulation circulation){
        return success(sampleService.flumeBack(circulation.getIds(), circulation.getUserName()));
    }
    @GetMapping("lz")
    public AjaxResult listLz(Long id){
        SampleLz sampleLz = new SampleLz();
        sampleLz.setSampleId(id);
        List<SampleLz> sampleLzs = sampleService.listLz(sampleLz);
        return success(sampleLzs);
    }
}

